package com.rpa.server.servicerpaserver.crawler.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.rpa.secure.servicerpasecure.common.constant.MessageConstant;
import com.rpa.secure.servicerpasecure.common.exception.TaskException;
import com.rpa.secure.servicerpasecure.common.util.CurrentTeacherInfoUtil;
import com.rpa.server.servicerpaserver.crawler.entity.Clazz;
import com.rpa.server.servicerpaserver.crawler.entity.Score;
import com.rpa.server.servicerpaserver.crawler.entity.dto.ActiveDto;
import com.rpa.server.servicerpaserver.crawler.operate.ActiveOperate;
import com.rpa.server.servicerpaserver.crawler.operate.ClazzOperate;
import com.rpa.server.servicerpaserver.crawler.service.ClazzService;
import com.rpa.server.servicerpaserver.crawler.service.ScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName ActiveController
 * @Description TODO
 * @Author 86152
 * @Date 2025/1/28 19:00
 * @Version 1.0
 **/
@Tag(name = "获取签到信息相关接口", description = "ActiveCrawlerController")
@Slf4j
@RestController
@RequestMapping("/crawler")
public class ActiveCrawlerController {
    @Autowired
    private ActiveOperate activeOperate;
    @Autowired
    private ScoreService scoreService;
    @Autowired
    private ClazzService clazzService;
    @Autowired
    private ClazzOperate clazzOperate;


    @PostMapping("/getActive")
    @Operation(summary = "获取签到信息", description = "getActive")
    public Boolean getActive(@RequestBody ActiveDto active) {
        if (active == null) {
            log.error("请求体中的 user 对象为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }
        String rpaRegisterId = active.getAccountId();

        // 检查 rpaRegisterId 是否为空
        if (rpaRegisterId == null) {
            log.error("请求体中的 rpaRegisterId 字段为空");
            throw new TaskException(MessageConstant.INVALID_REQUEST_BODY);
        }

        log.info("当前用户id为：{}", CurrentTeacherInfoUtil.getCurrentTeacherId());
        log.info("接收到的 rpaRegisterId 为：{}", rpaRegisterId);
        Boolean join = activeOperate.getActive(active).join();
        if (join){
            LambdaUpdateWrapper<Score> eq = new LambdaUpdateWrapper<Score>()
                    .eq(Score::getAccountId, rpaRegisterId)
                    .eq(Score::getCourseId, active.getCourseId())
                    .eq(Score::getClazzId, active.getClassId())
                    .eq(Score::getScoreType, 3);
             scoreService.remove(eq);
            LambdaQueryWrapper<Clazz> eq1 = new LambdaQueryWrapper<Clazz>()
                    .eq(Clazz::getAccountId, rpaRegisterId)
                    .eq(Clazz::getCourseId, active.getCourseId())
                    .eq(Clazz::getClazzId, active.getClassId())
                    .eq(Clazz::getIsDeleted, 0);

            List<Clazz> list = clazzService.list(eq1);
            List<Score> scoreActive = clazzOperate.getScoreActive(list);
            if (scoreActive.size()>0){
                 scoreService.saveBatch(scoreActive);
            }
        }
        return join;
    }
}
